package businessServlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.*;

import validate.Validate;

import com.google.appengine.api.datastore.Email;
import com.google.appengine.api.datastore.PhoneNumber;
import com.google.appengine.api.datastore.Text;


import controller.Controller;
import dataBase.Order;

@SuppressWarnings("serial")
public class NewOrderBusiness extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.sendRedirect("newOrder.jsp");
	}
	
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		
		try{
			int orderID = Integer.parseInt(req.getParameter("orderID"));		
			int currentStatus = Integer.parseInt(req.getParameter("currentStatus"));
			String destAddress = req.getParameter("destAddress");
			String comments = req.getParameter("comments");
			String userName = (String) req.getSession().getAttribute("userNameBusiness");
			PhoneNumber phone = new PhoneNumber(req.getParameter("phone"));
			Email email = new Email(req.getParameter("email"));
			String details = req.getParameter("details");
			String sendMail = req.getParameter("sendMail");
			int priority = 1;
			try{
				priority = Integer.parseInt(req.getParameter("priority"));
			}
			catch(Exception ex){}
			
			boolean sendMailBoolean = false;
			
			if (sendMail!=null && sendMail.equals("on"))
				sendMailBoolean = true;
			
			//System.out.println("%%%%%%%" + orderID + " "+currentStatus + " "+comments+ " "+ userName);
			if (Validate.phoneValid(phone.getNumber()))
			{
				if (email.getEmail().trim().equals("") || Validate.emailValid(email.getEmail()))
				{
					
					PhoneNumber newPhone = new PhoneNumber(phone.getNumber().replaceAll("-", ""));
					int result = Controller.getInstance().newOrder(orderID, currentStatus, new Text(comments), destAddress, userName,newPhone,email, sendMailBoolean,details,priority);
					
					System.out.println("result "+result);
					
					switch (result)
					{
					case 0:
						req.getSession().removeAttribute("validErrorNewOrder");
						resp.sendRedirect("allOrders.jsp");
						break;
					case 1:
						req.getSession().setAttribute("validErrorNewOrder","order id is already exists. please choose another order id.");		
						resp.sendRedirect("allOrders.jsp");
						break;				
					}
					
				}
				else
				{
					req.getSession().setAttribute("validErrorNewOrder","invalid email format");		
					resp.sendRedirect("allOrders.jsp");					
				}
				
			}
			else
			{
				req.getSession().setAttribute("validErrorNewOrder","invalid phone format");		
				resp.sendRedirect("allOrders.jsp");
			}

			
		}
		catch(NumberFormatException e)
		{
			req.getSession().setAttribute("validErrorNewOrder","order id is not a number");		
			resp.sendRedirect("allOrders.jsp");
			
		}
		
		

	}
}
